<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateTmsTables extends Migrator
{
    public function change(): void
    {
        $this->createShipmentCompanyTable();
        $this->createShipmentTable();
        $this->createShipmentTraceTable();
        $this->createPickupTaskTable();
    }

    private function createShipmentCompanyTable(): void
    {
        $this->table('shipment_company', [
            'comment' => '快递/物流公司',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 150,
                'comment' => '公司名称',
            ])
            ->addColumn('code', 'string', [
                'limit' => 80,
                'comment' => '公司编码',
            ])
            ->addColumn('contact_name', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '联系人',
            ])
            ->addColumn('contact_phone', 'string', [
                'limit' => 30,
                'default' => '',
                'comment' => '联系电话',
            ])
            ->addColumn('contact_email', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '联系邮箱',
            ])
            ->addColumn('address', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '办公地址',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-停用 1-启用',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['code'], ['unique' => true])
            ->addIndex(['status'])
            ->create();
    }

    private function createShipmentTable(): void
    {
        $this->table('shipments', [
            'comment' => '物流运单',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('pickup_task_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '揽件任务ID',
            ])
            ->addColumn('shipment_no', 'string', [
                'limit' => 80,
                'comment' => '运单号',
            ])
            ->addColumn('company_id', 'integer', [
                'signed' => false,
                'comment' => '物流公司ID',
            ])
            ->addColumn('status', 'string', [
                'limit' => 30,
                'default' => 'created',
                'comment' => '状态 created/picked/in_transit/delivered',
            ])
            ->addColumn('weight', 'decimal', [
                'precision' => 10,
                'scale' => 2,
                'default' => 0,
                'comment' => '重量(kg)',
            ])
            ->addColumn('fee', 'decimal', [
                'precision' => 10,
                'scale' => 2,
                'default' => 0,
                'comment' => '运费',
            ])
            ->addColumn('sender_snapshot', 'json', [
                'null' => true,
                'comment' => '寄件人信息快照',
            ])
            ->addColumn('receiver_snapshot', 'json', [
                'null' => true,
                'comment' => '收件人信息快照',
            ])
            ->addColumn('created_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '创建人',
            ])
            ->addColumn('delivered_at', 'timestamp', [
                'null' => true,
                'comment' => '送达时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['shipment_no'], ['unique' => true])
            ->addIndex(['status'])
            ->create();
    }

    private function createShipmentTraceTable(): void
    {
        $this->table('shipment_traces', [
            'comment' => '物流轨迹',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('shipment_id', 'integer', [
                'signed' => false,
                'comment' => '运单ID',
            ])
            ->addColumn('status', 'string', [
                'limit' => 50,
                'comment' => '节点状态',
            ])
            ->addColumn('description', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '描述',
            ])
            ->addColumn('occurred_at', 'timestamp', [
                'comment' => '发生时间',
            ])
            ->addColumn('location', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '位置',
            ])
            ->addColumn('created_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '记录人',
            ])
            ->addTimestamps()
            ->addIndex(['shipment_id'])
            ->addIndex(['occurred_at'])
            ->create();
    }

    private function createPickupTaskTable(): void
    {
        $this->table('pickup_tasks', [
            'comment' => '揽件任务',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('order_id', 'integer', [
                'signed' => false,
                'comment' => '订单ID',
            ])
            ->addColumn('task_no', 'string', [
                'limit' => 60,
                'comment' => '任务编号',
            ])
            ->addColumn('service_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '负责客服',
            ])
            ->addColumn('company_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '快递公司ID',
            ])
            ->addColumn('status', 'string', [
                'limit' => 30,
                'default' => 'pending',
                'comment' => '状态 pending/assigned/picked/completed/cancelled',
            ])
            ->addColumn('scheduled_at', 'timestamp', [
                'null' => true,
                'comment' => '计划揽件时间',
            ])
            ->addColumn('completed_at', 'timestamp', [
                'null' => true,
                'comment' => '完成时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['order_id'])
            ->addIndex(['task_no'], ['unique' => true])
            ->addIndex(['service_user_id'])
            ->addIndex(['status'])
            ->create();
    }
}
